<template>
  <vue-modal
    @submitData="resetPassword"
    modalTitle="重置用户密码"
    modalName="resetPasswordModal"
    modalWidth="600"
    ref="resetPasswordModal">
    <div slot="content" class="form-box">
      <div class="item">
        用户名：{{entity.username}}
      </div>
      <div class="item">
        请输入新密码：
        <el-input v-model="entity.password" class="input-password"></el-input>
        <span class="tip" v-show="showTip">{{tipMessage}}</span>
      </div>
    </div>
  </vue-modal>
</template>

<script>

  import VueModal from 'components/common/modal/VueModal'

  import {resetPassword} from "../../../network/viewsRequest/user";

  export default {
    name: "ResetPasswordTemplate",
    components: {
      VueModal
    },
    data() {
      return {
        entity: {
          username: '',
          password: '',
        },
        tipMessage: '', // 错误信息
        showTip: false, // 是否显示tipMessage
      }
    },
    props: {
      userDataFromParent: {
        type: Object,
        default: function() {
          return {};
        }
      }
    },
    watch: {
      "userDataFromParent.username": function(newValue) {
        if (newValue) {
          this.entity.username = newValue;
        }
      }
    },
    methods: {
      show() {
        this.entity.username = '';
        this.entity.password = '';
        this.showTip = false;
        this.$refs.resetPasswordModal.show('resetPasswordModal');
      },
      close() {
        this.$refs.resetPasswordModal.closeModal();
      },
      resetPassword() {
        if (!this.entity.password) {
          this.showTip = true;
          this.tipMessage = '密码不能为空';
          return;
        }

        if (this.entity.password.length < 6 || this.entity.password.length > 20) {
          this.showTip = true;
          this.tipMessage = '密码的长度在6到20位之间';
          return;
        }

        this.$confirm(`确定要重置用户${this.entity.username}的密码吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          resetPassword(this.entity).then(res => {
            if (res.code == 1) {
              this.$message.success('密码重置成功！');
              this.close();
              this.$emit("reloadTable");
            } else {
              this.$message.error(res.message);
            }
          })
        }).catch(() => {
          this.$message.info("操作已被取消");
        })

      }
    }
  }
</script>

<style scoped>
  .form-box {
    padding-left: 20px;
    padding-right: 20px;
  }
  .item {
    font-size: 18px;
    font-weight: bold;
    margin-bottom: 30px;
  }
  .item:last-child {
    margin-bottom: 0;
  }
  .input-password {
    margin-top: 10px;
  }
  .tip {
    margin-top: 10px;
    display: block;
    color: red;
    font-size: 12px;
  }
</style>